<?php

FLEA::loadClass('FLEA_Controller_Action');

class Controller_Photos extends FLEA_Controller_Action
{
	
	function actionIndex()
	{
		$viewdata = array(
			'title' => '相册管理',
	    );
		$this->_executeView('Photos.html', $viewdata);					
	}
	
	// ajax创建新相册
	function actionCreateAlbum(){
		if($this->_isAjax()){
			$tb_albums =& FLEA::getSingleton('Table_Albums');
			/*@var $tb_albums Table_Albums*/
			// 获得当前用户
			$user = $this->_dispatcher->getUser();
			
			// 整理数据
			$album['name'] = $_GET['name'];
			$album['member_id'] = $user['member_id'];
			
			echo $tb_albums->create($album);
		}
	}
	
	// 更新相册的名称
	function actionRenameAlbum(){
		if($this->_isAjax() and isset($_GET['album_id']) and isset($_GET['name']) ){
			$tb_albums =& FLEA::getSingleton('Table_Albums');
			/*@var $tb_albums Table_Albums*/
			$row = array(
				'album_id' => $_GET['album_id'],
				'name' => $_GET['name']
			);
			echo $tb_albums->update($row);
		}
	}
	
	// 删除一个相册
	function actionRemoveAlbum(){
		if($this->_isAjax()){
			// 获取登录用户
			$user = $this->_dispatcher->getUser();
			// 获取传来的相册ID信息
			$tb_albums =& FLEA::getSingleton('Table_Albums');
			/*@var $tb_albums Table_Albums*/
			$album = $tb_albums->find($_GET['album_id']);
			
			// 检验当前用户能否操作该相册
			if ($album and $album['member_id']==$user['member_id']){
				
				// 获取该相册的图片列表
				$tb_photos =& FLEA::getSingleton('Table_Photos');
				/*@var $tb_photos Table_Photos*/
				$condition = "album_id = {$album['album_id']}";
				$photos = $tb_photos->findAll($condition);
				
				// 删除这些图片
				$prefix = date('Y-m');
				$photoDir = FLEA::getAppInf('uploadDir') . DS . $user['member_id'] . DS . $prefix . DS;
				foreach ($photos as $value) {
			        $photoFilename = $photoDir . $value['photo'];
			        $thumbFilename = $photoDir . substr($value['photo'],0,-4) . '_small.jpg';
			        // 尝试删除图片
			        @unlink($photoFilename);
			        @unlink($thumbFilename);
				}
				// 尝试删除目录
			    @rmdir($photoDir);
			    
			    // 删除图片记录
			    $tb_photos->removeByConditions($condition);
				
				// 删除相册记录
				$tb_albums->removeByPkv($album['album_id']);
				echo true;
			}
		}
	}
	
	// 更新照片信息
	function actionUpdatePhoto(){
		if($this->_isAjax()){
			$tb_photos =& FLEA::getSingleton('Table_Photos');
			/*@var $tb_photos Table_Photos*/
			$_POST['description'] = $_POST['description'];
			echo $tb_photos->update($_POST);
			echo $_POST['description'];
		}
	}
	
	function actionMovePhoto(){
		
	}
	
	// 删除一张照片
	function actionRemovePhoto(){
		if ($this->_isAjax()){
			// 获取照片数据
			$tb_photos =& FLEA::getSingleton('Table_Photos');
			/*@var $tb_photos Table_Photos*/
			$photo = $tb_photos->find($_GET['photo_id']);
			
			// 获取用户信息
			$user = $this->_dispatcher->getUser();
			
			// 获取所属相册数据
			$tb_albums =& FLEA::getSingleton('Table_Albums');
			/*@var $tb_albums Table_Albums*/
			$album = $tb_albums->find($photo['album_id']);
			
			// 验证照片是否有权删除
			if ($photo && $album['member_id']==$user['member_id']){
				
				// 删除图片
				$prefix = date('Y-m',$photo['created']);
				$photoDir = FLEA::getAppInf('uploadDir') . DS . $user['member_id'] . DS . $prefix . DS;
				$photoFilename = $photoDir . $photo['photo'];
			    $thumbFilename = $photoDir . substr($photo['photo'],0,-4) . '_small.jpg';
		        @unlink($photoFilename);
		        @unlink($thumbFilename);
		        
		        // 删除照片数据记录
				$tb_photos->removeByPkv($photo['photo_id']);
		        				
				// 如果被删除的图是相册封面，则替代或清空相册封面
				if($album['coverpic']==$thumbFilename){
					// 相册中还有其他图吗？
					$newCoverpic = $tb_photos->find("album_id = {$album['album_id']}","created DESC");
					if($newCoverpic){
						// 选择图册中的另一张图做封面
						$album['coverpic'] = FLEA::getAppInf('uploadDir') . DS . $user['member_id'] . DS
												. date('Y-m',$newCoverpic['created']) . DS
												. substr($newCoverpic['photo'],0,-4) . '_small.jpg';
						$tb_albums->update($album);
					}else {
						$album['coverpic'] = null;
						$tb_albums->update($album);
					}
				}
		        
				echo true;
			}
		}
	}
	
	// ajax返回相册列表
	function actionAlbumList(){
		if($this->_isAjax()){
			$tb_albums =& FLEA::getSingleton('Table_Albums');
			/*@var $tb_albums Table_Albums*/
			// 获得当前用户
			$user = $this->_dispatcher->getUser();
			// 获取数据
			$albums = $tb_albums->findAll("member_id = '{$user['member_id']}'");
			echo json_encode($albums);
		}
	}
	
	// ajax返回图片列表
	function actionPhotoList(){
		if($this->_isAjax()){
			// 获取页码
			$page = isset($_GET['pg'])?$_GET['pg']:0;
			
			// 获取每页记录数
			$size = isset($_GET['size'])?$_GET['size']:10;
			
			// 获得当前用户
			$user = $this->_dispatcher->getUser();
			
			// 获得当前相册
			$album_id = $_GET['album_id'];
			
			$tb_photos =& FLEA::getSingleton('Table_Photos');
			/*@var $tb_photos Table_Photos*/
			
			FLEA::loadClass('FLEA_Helper_Pager');
			$pager =& new FLEA_Helper_Pager($tb_photos, $page, $size, "album_id = '{$album_id}'");
			/*@var $pager FLEA_Helper_Pager*/
			
			// 获得数据
			$rows = $pager->findAll();
			
			// 整理数据
			$uploadDir = FLEA::getAppInf('uploadDir');
			$photos = array();
			foreach ($rows as $key=>$value) {
				$photos[$key]['photo_id'] = $value['photo_id'];
				$photos[$key]['dir'] = $uploadDir . DS . $user['member_id'] . DS . date('Y-m',$value['created']) . DS;
				$photos[$key]['s'] = substr($value['photo'],0,-4) . '_small.jpg';
				$photos[$key]['b'] = $value['photo'];
				$photos[$key]['name'] = $value['name'];
				$photos[$key]['description'] = $value['description'];
				
			}
			$nav = $pager->getPagerData();
			//dump(array('nav'=>$nav,'photos'=>$photos));
			echo json_encode(array('nav'=>$nav,'photos'=>$photos));
		}
	}
	
	
}

?>